package org.example.scoresystem_backen.config;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.example.scoresystem_backen.entity.Admin;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Admin.Role枚举类型处理器
 * 用于MyBatis中Admin.Role枚举与数据库字符串值的转换
 */
public class AdminRoleTypeHandler extends BaseTypeHandler<Admin.Role> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Admin.Role parameter, JdbcType jdbcType) throws SQLException {
        // 将枚举转换为数据库存储的字符串值
        ps.setString(i, parameter.getValue());
    }

    @Override
    public Admin.Role getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String value = rs.getString(columnName);
        return value == null ? null : Admin.Role.fromValue(value);
    }

    @Override
    public Admin.Role getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String value = rs.getString(columnIndex);
        return value == null ? null : Admin.Role.fromValue(value);
    }

    @Override
    public Admin.Role getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String value = cs.getString(columnIndex);
        return value == null ? null : Admin.Role.fromValue(value);
    }
}
